<?php
namespace Admin\Controller;

use Think\Controller;

class UserController extends Controller {
    private $user;
    public function __construct()
    {
        parent::__construct();
        $this->user = M("user");

    }
    function register(){
        $this->display();
    }
    function do_register(){
           $User=D("user");
        if (!$User->create()){
           $this->error($User->getError());

        }else {
            $User->add();
            //echo $this->user->getLastSql();
            $this->success("注册成功",U("login"));
        }

    }
    function login(){
        $this->display();
    }
    function  do_login(){
        $code = I("post.code");
        if (!$this->check_verify($code)) {
            $this->error('验证码错误');
        }
        if (IS_POST){
            $data['email']=I("post.email");
            $data['password']=substr(md5(I("post.password")),6,-6);
            $user=$this->user->where($data)->find();
            if (!$user){
                $this->error("账号或密码错误");
            }
              if (I('post.rem')){
                  setcookie('token',$user['token'],time() + 7 * 24 * 60 *60,'/');

              }
                $_SESSION['user']=$user;
                $this->success("登入成功",U("Admin/Index/index"));
        }
    }
    public function verify(){

        $Verify = new \Think\Verify(C('verify'));//C去conf里面的值
        $Verify->entry();
    }
    function logout()
    {
        $_SESSION = array();

        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), "", time() - 3600, "/");
            setcookie("token","",time() - 3600 ,"/");
        }

        $this->success("你已经安全退出",U("login"));
    }
    //检测验证码是否正确
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }


}
